package com.my.helper.fragment;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.my.helper.R;
import com.my.helper.adapter.MyFragmentPagerAdapter;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 项目名称：Helper
 * 创建人：
 * 创建时间：2018/5/4 11:29
 */

public class FragmentMain extends Fragment {
    private View view = null;
    /*** 标题文字 */
    private static final String[] CHANNELS = new String[]{"养生", "饮食", "健康"};
    private List<String> mDataList = Arrays.asList(CHANNELS);
    /*** 游标 */
    private MagicIndicator magicIndicator;
    /*** ViewPager */
    private ViewPager mPager;
    private ArrayList<Fragment> fragmentsList;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_main, null);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        fragmentsList = new ArrayList<Fragment>();
        fragmentsList.add(new FragmentYangSheng());
        fragmentsList.add(new FragmentShiWu());
        fragmentsList.add(new FragmentJianKang());

        mPager = view.findViewById(R.id.vPager);
        mPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), fragmentsList));


        // 新的游标
        magicIndicator = view.findViewById(R.id.magic_indicator1);
        CommonNavigator commonNavigator = new CommonNavigator(getActivity());
//        // 设置平分
        commonNavigator.setAdjustMode(true);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mDataList == null ? 0 : mDataList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mDataList.get(index));
                simplePagerTitleView.setNormalColor(Color.parseColor("#d3d3d3"));
                simplePagerTitleView.setSelectedColor(Color.WHITE);
                simplePagerTitleView.setTextSize(16);
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                // 游标设置
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                // 设置游标圆角半径
                indicator.setRoundRadius(2);
                // 设置游标宽度
                indicator.setLineWidth(UIUtil.dip2px(context, 34));
                // 设置游标颜色
                indicator.setColors(Color.parseColor("#FFFFFF"));
                return indicator;
            }
        });

        magicIndicator.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator, mPager);
    }
}
